package com.shuma.approval.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuma.approval.constant.OperatorStatusVOConstant;
import com.shuma.approval.constant.RedisConstants;
import com.shuma.approval.dto.R;
import com.shuma.approval.entity.OperatorLog;
import com.shuma.approval.mapper.OperatorLogMapper;
import com.shuma.approval.service.IOperatorLogService;
import com.shuma.approval.vo.OperatorLogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 * 操作记录表
 服务实现类
 * </p>
 *
 * @author xsj
 * @since 2024-06-17
 */
@Service
public class OperatorLogServiceImpl extends ServiceImpl<OperatorLogMapper, OperatorLog> implements IOperatorLogService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public R VOList(Long id) {
        String key = RedisConstants.OPERATORLOG+id;
        //从redis查询操作记录缓存
        String VOListJSON = stringRedisTemplate.opsForValue().get(key);
        //判断是否存在
        if(StrUtil.isNotBlank(VOListJSON)) {
            //存在返回
            JSONArray jsonArray = JSONUtil.parseArray(VOListJSON);
            List<OperatorLogVO> VOList = new ArrayList<>();
            // 遍历JSONArray并转换为OperatorLogVO对象
            jsonArray.forEach(json -> VOList.add(JSONUtil.toBean((JSONObject) json, OperatorLogVO.class)));
            return R.ok(VOList);
        }
        if (VOListJSON!=null){
            return R.fail("物品信息不存在");
        }
        //不存在查询数据库
        LambdaQueryWrapper<OperatorLog> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OperatorLog::getCommodityId,id).orderByDesc(OperatorLog::getOperatorTime);
        List<OperatorLog> list = list(lambdaQueryWrapper);
        //数据库不存在存空值到redis
        if (list.isEmpty()){
            stringRedisTemplate.opsForValue().set(key,"",RedisConstants.CACHE_NULL_TTL, TimeUnit.MINUTES);
            return R.fail("物品信息不存在");
        }
        //创建VO对象返回前端
        List<OperatorLogVO> VOList = list.stream().map(log -> {
            OperatorLogVO vo = new OperatorLogVO();
            vo.setOperateId(log.getOperateId());
            vo.setOperatorTime(log.getOperatorTime());
            vo.setRemark(log.getRemark());
            vo.setOwner(log.getOwner());
            // 根据 operatorStatus 设置 operateId
            switch (log.getOperatorStatus()) {
                case 1:
                    vo.setOperatorStatus(OperatorStatusVOConstant.ADD_GOOD);
                    break;
                case 2:
                    vo.setOperatorStatus(OperatorStatusVOConstant.GO_OFFLINE);
                    break;
                case 3:
                    vo.setOperatorStatus(OperatorStatusVOConstant.GO_ONLINE);
                    break;
                case 4:
                    vo.setOperatorStatus(OperatorStatusVOConstant.ADD_APPROVAL);
                    break;
                case 5:
                    vo.setOperatorStatus(OperatorStatusVOConstant.PASS_APPROVAL);
                    break;
                case 6:
                    vo.setOperatorStatus(OperatorStatusVOConstant.OUT_APPROVAL);
                    break;
                default:
                    vo.setOperatorStatus("");
                    break;
            }
            return vo;
        }).collect(Collectors.toList());
        //存在，写入redis
        stringRedisTemplate.opsForValue().set(key,JSONUtil.toJsonStr(VOList),RedisConstants.OPERATORLOG_TTL, TimeUnit.MINUTES);
        return R.ok(VOList);
    }
}
